



var oEditors = [];
function onSubmit() {

  oEditors.getById["text"].exec("UPDATE_CONTENTS_FIELD", []);
  
}

$(document).ready(function(){
    nhn.husky.EZCreator.createInIFrame({
        oAppRef: oEditors,
        elPlaceHolder: "text",
        sSkinURI: "../se2/SmartEditor2Skin.html",        
        fCreator: "createSEditor2"
    });
    
    $(".date").datepicker({

		prevText: '이전 달',
	      nextText: '다음 달',
	      monthNames: ['1월','2월','3월','4월','5월','6월','7월','8월','9월','10월','11월','12월'],
	      monthNamesShort: ['1월','2월','3월','4월','5월','6월','7월','8월','9월','10월','11월','12월'],
	      dayNames: ['일','월','화','수','목','금','토'],
	      dayNamesShort: ['일','월','화','수','목','금','토'],
	      dayNamesMin: ['일','월','화','수','목','금','토'],
		dateFormat : "yy-mm-dd",
		changeMonth : true,
		changeYear : true,
		numberOfMonths:2,
		
	});
    
    
    $(".date_a_s").datepicker({

		prevText: '이전 달',
	      nextText: '다음 달',
	      monthNames: ['1월','2월','3월','4월','5월','6월','7월','8월','9월','10월','11월','12월'],
	      monthNamesShort: ['1월','2월','3월','4월','5월','6월','7월','8월','9월','10월','11월','12월'],
	      dayNames: ['일','월','화','수','목','금','토'],
	      dayNamesShort: ['일','월','화','수','목','금','토'],
	      dayNamesMin: ['일','월','화','수','목','금','토'],
		dateFormat : "yy-mm-dd",
		changeMonth : true,
		changeYear : true,
		numberOfMonths:2,
		onClose:function(selectedDate){
			if(selectedDate!=""){
			$(".date_a_e").datepicker("option","minDate",selectedDate);
			}
			
			if(selectedDate!="" &&($(".date_r_s").datepicker("option","minDate")==null || $(".date_end").datepicker("option","minDate")<selectedDate)){
				$(".date_r_s").datepicker("option","minDate",selectedDate);
			}
			
			if(selectedDate!="" &&($(".date_r_e").datepicker("option","minDate")==null || $(".date_end").datepicker("option","minDate")<selectedDate)){
				$(".date_r_e").datepicker("option","minDate",selectedDate);
			}
			
			if(selectedDate!="" &&($(".date_end").datepicker("option","minDate")==null || $(".date_end").datepicker("option","minDate")<selectedDate )){
				$(".date_end").datepicker("option","minDate",selectedDate);
			}
		}
	});
	$(".date_a_e").datepicker({

		prevText: '이전 달',
	      nextText: '다음 달',
	      monthNames: ['1월','2월','3월','4월','5월','6월','7월','8월','9월','10월','11월','12월'],
	      monthNamesShort: ['1월','2월','3월','4월','5월','6월','7월','8월','9월','10월','11월','12월'],
	      dayNames: ['일','월','화','수','목','금','토'],
	      dayNamesShort: ['일','월','화','수','목','금','토'],
	      dayNamesMin: ['일','월','화','수','목','금','토'],
		dateFormat : "yy-mm-dd",
		changeMonth : true,
		changeYear : true,
		numberOfMonths:2,
		onClose:function(selectedDate){
			
			if(selectedDate!=""){
			$(".date_a_s").datepicker("option","maxDate",selectedDate);
			}
			
			if(selectedDate!=""){
			$(".date_r_s").datepicker("option","minDate",selectedDate);
			}
			if(selectedDate!="" &&($(".date_r_e").datepicker("option","minDate")==null || $(".date_r_e").datepicker("option","minDate")<selectedDate)){
				$(".date_r_e").datepicker("option","minDate",selectedDate);
			}
			
			if(selectedDate!="" &&($(".date_end").datepicker("option","minDate")==null || $(".date_end").datepicker("option","minDate")<selectedDate)){
				$(".date_end").datepicker("option","minDate",selectedDate);
			}
		}
	});
	
	$(".date_r_s").datepicker({
		prevText: '이전 달',
	      nextText: '다음 달',
	      monthNames: ['1월','2월','3월','4월','5월','6월','7월','8월','9월','10월','11월','12월'],
	      monthNamesShort: ['1월','2월','3월','4월','5월','6월','7월','8월','9월','10월','11월','12월'],
	      dayNames: ['일','월','화','수','목','금','토'],
	      dayNamesShort: ['일','월','화','수','목','금','토'],
	      dayNamesMin: ['일','월','화','수','목','금','토'],
		dateFormat : "yy-mm-dd",
		changeMonth : true,
		changeYear : true,
		numberOfMonths:2,
		onClose:function(selectedDate){

			if(selectedDate!="" &&($(".date_a_s").datepicker("option","maxDate")==null || $(".date_a_s").datepicker("option","maxDate")>selectedDate)){
				$(".date_a_s").datepicker("option","maxDate",selectedDate);
			}
			
			if(selectedDate!=""){
			$(".date_a_e").datepicker("option","maxDate",selectedDate);
			}
			
			if(selectedDate!=""){
			$(".date_r_e").datepicker("option","minDate",selectedDate);
			}
			if(selectedDate!="" &&($(".date_end").datepicker("option","minDate")==null || $(".date_end").datepicker("option","minDate")<selectedDate)){
				$(".date_end").datepicker("option","minDate",selectedDate);
			}

		}
	});
	$(".date_r_e").datepicker({
		prevText: '이전 달',
	      nextText: '다음 달',
	      monthNames: ['1월','2월','3월','4월','5월','6월','7월','8월','9월','10월','11월','12월'],
	      monthNamesShort: ['1월','2월','3월','4월','5월','6월','7월','8월','9월','10월','11월','12월'],
	      dayNames: ['일','월','화','수','목','금','토'],
	      dayNamesShort: ['일','월','화','수','목','금','토'],
	      dayNamesMin: ['일','월','화','수','목','금','토'],
		dateFormat : "yy-mm-dd",
		changeMonth : true,
		changeYear : true,
		numberOfMonths:2,
		onClose:function(selectedDate){
			
			if(selectedDate!="" &&($(".date_a_s").datepicker("option","maxDate")==null || $(".date_a_s").datepicker("option","maxDate")>selectedDate)){
				$(".date_a_s").datepicker("option","maxDate",selectedDate);
			}
			if(selectedDate!="" && ($(".date_a_e").datepicker("option","maxDate")==null || $(".date_a_e").datepicker("option","maxDate")>selectedDate)){
				$(".date_a_e").datepicker("option","maxDate",selectedDate);
			}
			
			if(selectedDate!=""){
			$(".date_r_s").datepicker("option","maxDate",selectedDate);
			}

			if(selectedDate!=""){
				$(".date_end").datepicker("option","minDate",selectedDate);
			}
		}
	});
	$(".date_end").datepicker({
		prevText: '이전 달',
	      nextText: '다음 달',
	      monthNames: ['1월','2월','3월','4월','5월','6월','7월','8월','9월','10월','11월','12월'],
	      monthNamesShort: ['1월','2월','3월','4월','5월','6월','7월','8월','9월','10월','11월','12월'],
	      dayNames: ['일','월','화','수','목','금','토'],
	      dayNamesShort: ['일','월','화','수','목','금','토'],
	      dayNamesMin: ['일','월','화','수','목','금','토'],
		dateFormat : "yy-mm-dd",
		changeMonth : true,
		changeYear : true,
		numberOfMonths:2,
		onClose:function(selectedDate){
			if(selectedDate!="" &&($(".date_a_s").datepicker("option","maxDate")==null || $(".date_a_s").datepicker("option","maxDate")>selectedDate)){
				$(".date_a_s").datepicker("option","maxDate",selectedDate);
			}
			if(selectedDate!="" &&($(".date_a_e").datepicker("option","maxDate")==null || $(".date_a_e").datepicker("option","maxDate")>selectedDate)){
				$(".date_a_e").datepicker("option","maxDate",selectedDate);
			}
			if(selectedDate!="" &&($(".date_r_s").datepicker("option","maxDate")==null || $(".date_r_s").datepicker("option","maxDate")>selectedDate)){
				$(".date_r_s").datepicker("option","maxDate",selectedDate);
			}
			
			if(selectedDate!=""){
			$(".date_r_e").datepicker("option","maxDate",selectedDate);
			}
		}

	});
	

    $(".header").load("header.html"); 
	  $(".footer").load("footer.html"); 
    
    $("form").submit(onSubmit);
	
    
    
});
function createContest(){
	
	var message = confirm("정말 이대로 하시겠습니까?") ? true: false;
	
	if(message)
		alert("저장 되었습니다.");
	
}


function numObj(e){
    if (window.event){
        if((event.keyCode >= 48 && event.keyCode <= 57) || (event.keyCode>=96 && event.keyCode<=105) || event.keyCode==8 || event.keyCode==9 || event.keyCode==13 ||event.keyCode==37||event.keyCode==39||
        		event.keyCode==46||event.keyCode==117){ //숫자키만 입력
            return true;
        }
        else{
        	e.preventDefault();
        }
    }
    else if (e){//숫자와 백스페이스키
        if((e.which >= 48 && e.which <= 57) (e.which>=96 && e.which<=105) || e.which==8 || e.which==9 || e.which==13 ||e.which==37||e.which==39||
        		e.which==46||e.which==117 ){ //숫자키만 입력
            return true;
        }
        else{
            e.preventDefault();
        }
    }
}

var myModule = angular.module('myModule', []);
	myModule.filter('range', function() {
	  return function(input, total) {
	    total = parseInt(total);
	    for (var i=0; i<total; i++)
	      input.push(i);
	    return input;
	  };
	});
	
	


	myModule.directive('datepicker', function() {
		   return function(scope, element, attrs) {
			   
		       element.datepicker({
		           inline: true,
		           dateFormat: 'yy-mm-dd',
		           prevText: '이전 달',
		 	      nextText: '다음 달',
		 	      monthNames: ['1월','2월','3월','4월','5월','6월','7월','8월','9월','10월','11월','12월'],
		 	      monthNamesShort: ['1월','2월','3월','4월','5월','6월','7월','8월','9월','10월','11월','12월'],
		 	      dayNames: ['일','월','화','수','목','금','토'],
		 	      dayNamesShort: ['일','월','화','수','목','금','토'],
		 	      dayNamesMin: ['일','월','화','수','목','금','토'],
		           
		           onSelect: function(dateText) {
		              
		               scope.$apply(function(){
		            	   scope.selectedDate.date=dateText;
		               });
		           }
		       
		       
		       });
		   }
		});
	
	
    myModule.controller('MyController', function ($scope, $http,$location) {
    	
        $scope.items = [];
        $scope.contestItems=[];
        $scope.user={};
        $scope.trackCount=1;
        $scope.dateCount=0;
        $scope.selectedDate=[];
        $scope.date=[];
        $scope.checkCount=null;
        $scope.checkName=null;
        $scope.check=true;
        
        $scope.trackItems=[];
        
        
        $scope.loadData = function() {
        	 
        myUtil.onHttp($http,'get','contestAll.do',null,null,
        			function(data){
        				$scope.contestItems = data;
        				$scope.contest_id=$scope.contestItems[0].contest_id;
        			}
        	)
        }
        
        $scope.$watch('count  + contest_id',function(){
        	if($scope.count!=null && $scope.count!="" && $scope.contest_id!=null){
        		
        		myUtil.onHttp($http,'get','countCheck.do',{contest_id:$scope.contest_id,count:$scope.count},null,
        				function(data){
        					$scope.checkCount=data;
        					
        				}
        		)
        	}else{
        		$scope.checkCount=null;
        	}
        	
        	
        })
       
        $scope.$watch('countest_id',function(){
        	$scope.name=null;
        	$scope.checkName=null;
        })
        
        $scope.$watch('checkCount + checkName + count + contest_id',function(){
        	$scope.total();
        })
        
  
        
        $scope.total=function(){
        	
        	var check=true;
        	
        	for( var i=0 ; i<$scope.trackItems.length ; ++i)
        		if($scope.trackItems[i].track==null || $scope.trackItems[i].track=="")
        			check=false;
        	
        	
        	if( $scope.check && check && $('input[name=tracks]').val()!="" && $('input[name=tracks]').val()!=null  &&($scope.checkName=='true' || $scope.contest_id!="") && ($scope.checkCount=='true' ||($scope.count!="" && $scope.count!=null) )&&
        			( $("input[name=name]").val()!='') && $(".date_a_s").val()!=""&&($(".date_a_e").val()!="")&&($(".date_r_s").val()!="")&&($(".date_r_e").val()!="")&&($(".date_end").val()!="")){
        			$('input.save').attr('disabled', false);
        		}else{
        			
        			$('input.save').attr('disabled', true);
        		}
        	
        	if(!$scope.check){
        		$scope.check=true;
        	}
        }
        
        $scope.nameCheck=function(){

        	if($scope.name!=null && $scope.name!="" && ($scope.contest_id==null || $scope.contest_id=='')){
        		myUtil.onHttp($http,'get','contestNameCheck.do',{name:$scope.name},null,
        				function(data){
        					$scope.checkName=data;
        					$scope.total();
        				}
        		)
        	}else{
        		$scope.checkName=null;
        	}
        }
       
       
       $scope.removeDate=function(index){

    	   $scope.date.splice(index,1);
    	   
       }
       $scope.addDate=function(){
    	   $scope.dateCount=$scope.dateCount+1;
    	   $scope.date.push({name:null,date:null,dateCount:$scope.dateCount});
       }
       
       $scope.addTrack=function(){
    	   $scope.trackCount=$scope.trackCount+1;
    	   $scope.trackItems.push({trackCount:$scope.trackCount,track:null});
       }
       $scope.removeTrack=function(index){
       	$scope.trackItems.splice(index,1);
       	$scope.total();
       }

       $scope.loadData();
       $scope.addTrack();
       
    });